package controller.expertInfo.checkExpertInfoDlg;

import StaticFinal.CrudType;
import controller.common.StaticConstant;
import exception.WTException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import services.expertInfo.LeaderCheckServices;
import util.JsonUtil;
import util.ObjUtil;
import wt.entity.expertInfo.*;
import wt.staticData.TableName;

import java.util.Map;

/**
 * Created by Administrator on 2017/12/2.
 */
@Controller
@RequiresPermissions("expertInfo:checkExpertInfoDlg:default")

@RequestMapping("expertInfo/checkExpertInfoDlg")
public class CheckExpertInfoDlgController {
    @Autowired
    LeaderCheckServices leaderCheckServices;


    @RequestMapping("showDetail")
    public String showDetail(String id, Map map) throws Exception {
        LeaderCheckWithBLOBs leaderCheckWithBLOBs = leaderCheckServices.selectByPrimaryKey(id);

        if (TableName.EMS_EXPERT_BASE.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertBase.class, map);
            return "expertInfo/checkExpertInfoDlg/expertBaseDlg";
        } else if (TableName.EMS_EXPERT_CERTIFICATE.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertCertificate.class, map);
            return "expertInfo/checkExpertInfoDlg/expertCertificateDlg";
        } else if (TableName.EMS_EXPERT_COMPANY.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertCompany.class, map);
            return "expertInfo/checkExpertInfoDlg/expertCompanyDlg";
        } else if (TableName.EMS_EXPERT_EDUCATION.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertEducation.class, map);
            return "expertInfo/checkExpertInfoDlg/expertEducationDlg";
        } else if (TableName.EMS_EXPERT_MAJOR.equals(leaderCheckWithBLOBs.getTableName())) {
            map.put("data", leaderCheckWithBLOBs.getNewData());
            return "expertInfo/checkExpertInfoDlg/expertMajorDlg";
        } else if (TableName.EMS_EXPERT_REVIEW.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertReview.class, map);
            return "expertInfo/checkExpertInfoDlg/expertReviewDlg";
        } else if (TableName.EMS_EXPERT_TRAIN.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertTrain.class, map);
            return "expertInfo/checkExpertInfoDlg/expertTrainDlg";
        } else if (TableName.EMS_EXPERT_WORK.equals(leaderCheckWithBLOBs.getTableName())) {
            bindData(leaderCheckWithBLOBs, ExpertWork.class, map);
            return "expertInfo/checkExpertInfoDlg/expertWorkDlg";
        } else {
            map.put(StaticConstant.ERROR_MESSAGE, "出现异常数据，请刷新页面！");
            return StaticConstant.ERROR_DLG;
        }

    }

    private void bindData(LeaderCheckWithBLOBs leaderCheckWithBLOBs, Class clazz, Map map) throws Exception {
        if (leaderCheckWithBLOBs.getOperateType() == CrudType.insert) {
            map.put("data", JsonUtil.json2Obj(leaderCheckWithBLOBs.getNewData(), clazz));
        } else if (leaderCheckWithBLOBs.getOperateType() == CrudType.update) {
            Object oldData = JsonUtil.json2Obj(leaderCheckWithBLOBs.getOldData(), clazz);
            Object newData = JsonUtil.json2Obj(leaderCheckWithBLOBs.getNewData(), clazz);
            ObjUtil.insertSelectiveGETSET(newData, oldData);
            map.put("data", oldData);
            map.put("newData", newData);
        } else if (leaderCheckWithBLOBs.getOperateType() == CrudType.delete) {
            map.put("data", JsonUtil.json2Obj(leaderCheckWithBLOBs.getOldData(), clazz));
        } else {
            throw new WTException("出现未知操作类型");
        }
        map.put("leaderCheck", leaderCheckWithBLOBs);

    }


}
